package edu.chen.service.impl;

import edu.chen.bean.User;
import edu.chen.dao.impl.UserDaoImplByHibernate;
import edu.chen.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service("userServiceImplByHibernate")
public class UserServiceImplByHibernate implements UserService {

    private UserDaoImplByHibernate userDaoImplByHibernate;

    @Autowired
    public void setUserDaoImplByHibernate(UserDaoImplByHibernate userDaoImplByHibernate) {
        this.userDaoImplByHibernate = userDaoImplByHibernate;
    }

    public boolean addUser(User user) {
        if(exist(user.getUsername())){
            return false;
        }
        userDaoImplByHibernate.insert(user);
        return true;
    }

    public boolean deleteUser(String username) {
        if(!exist(username)){
            return false;
        }
        userDaoImplByHibernate.delete(username);
        return true;
    }

    public boolean exist(String username) {
        return userDaoImplByHibernate.query(username) != null ? true : false;
    }

    public boolean verifyUser(User user) {
        if (!exist(user.getUsername())){
            return false;
        }
        return userDaoImplByHibernate.query(user.getUsername()).getPassword().equals(user.getPassword())?true:false;
    }
}
